package com.atguigu.gulimall.product.controller;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVO;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author ls
 * @email 1725248162@qq.com
 * @date 2021-06-30 10:20:10
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * /product/attrgroup/{catelogId}/withattr
     * 查询这个分类下所有属性分组及属性
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R listAttrGroupWithAttrsByCatelogId(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVO> list = attrGroupService.listAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", list);
    }

    /**
     * product/attrgroup/5/attr/relation
     * 查询 与这个属性分组 5 关联的属性集合
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R listAttrOfAttrGroup(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> list = attrGroupService.listAttrOfAttrGroup(attrGroupId);

        return R.ok().put("data", list);
    }

    /**
     * product/attrgroup/attr/relation/delete
     * @param list
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrGroupRelationVO> list) {
        attrGroupService.deleteRelation(list);
        return R.ok();
    }

    /**
     * /product/attrgroup/5/noattr/relation
     * 查询当前分组的分类下没有关联且没有被其他分组引用的属性
     * @param attrGroupId
     * @param params
     * @return
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R listAttrOfAttrGroupCategoryNoRef(@PathVariable("attrGroupId") Long attrGroupId,
                                              @RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.listAttrOfAttrGroupCategoryNoRef(attrGroupId, params);
        return R.ok().put("page", page);
    }

    /**
     * 关联属性分组 和 属性
     * @param relationEntities
     * @return
     */
    @PostMapping("/attr/relation")
    public R attrGroupRelationAttr(@RequestBody List<AttrAttrgroupRelationEntity> relationEntities) {
        attrAttrgroupRelationService.saveBatch(relationEntities);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getArrtGroupById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
